/*
 * Server.h
 *
 *  Created on: Aug 11, 2012
 *      Author: nam20485
 */

#ifndef ISERVER_H_
#define ISERVER_H_

#include "IConnector.h"
#include "Connection.h"
#include "Message.h"

class IServer
{
public:
	typedef enum
	{
		CLIENT_DISCONNECTED,
		SUCCESS,
		FAIL,
		RECEIVE_FAILED,
		SEND_FAILED
	} RESPONSE;

	typedef void (*ServerReceiveHandler)(Connection::ID lClientID, const Message& message);

	IServer(/*ServerReceiveHandler m_pfnReceiveHandler*/);
	virtual ~IServer();

	Connection::ID Connect(IConnector& connector);
	RESPONSE Shutdown();

	RESPONSE Send(const Message& message, Connection::ID connectionID = 0);
	const Message* Receive(Connection::ID connectionID = 0);

	RESPONSE Serve(Connection::ID id = 0);
	RESPONSE Loop(Connection::ID id = 0);

protected:
	static const unsigned long MAX_CONNECTIONS = 5;

	unsigned long m_ulConnectionCount;
	Connection* m_apConnections[MAX_CONNECTIONS];
	//const ServerReceiveHandler m_pfnReceiveHandler;

	//void OnConnectionReceive(Connection::ID lClientID, const Message& message);

	inline const Connection* FindConnection(Connection::ID id) const;
	virtual RESPONSE HandleMessage(const Message& message, Connection::ID connection = 0) = 0;
};


#endif /* ISERVER_H_ */
